package main

import (
	"fmt"
	pb "ihos/ihos"
	"time"

	tm "github.com/golang/protobuf/ptypes/timestamp"
)

type denglu_shuju struct {
	id    int32  //PK
	dlzt  int32  //登录状态
	dlsj  int64  //登录时间, 秒
	dlyxq int64  //登录有效期，秒
	fwpz  string //访问凭证
	jslx  int32  //角色类型
	dlmc  string //登录名称
	aid   int32  //帐号PK
}

func (w *denglu_shuju) GetFromDB(id int32) error {
	sql := "SELECT id,dlzt,dlsj,dlyxq,fwpz,jslx,dlmc,aid FROM denglu_shuju WHERE id=$1"
	return DBConn.db.QueryRow(sql, id).Scan(&w.id, &w.dlzt, &w.dlsj, &w.dlyxq, &w.fwpz, &w.jslx, &w.dlmc, &w.aid)
}

func (w *denglu_shuju) GetFromDBByDlmc(dlmc string) error {
	sql := "SELECT id,dlzt,dlsj,dlyxq,fwpz,jslx,dlmc,aid FROM denglu_shuju WHERE dlmc='" + dlmc + "'"
	return DBConn.db.QueryRow(sql).Scan(&w.id, &w.dlzt, &w.dlsj, &w.dlyxq, &w.fwpz, &w.jslx, &w.dlmc, &w.aid)
}

func (w *denglu_shuju) Verify() error {
	// log.Println("Verify:", w)
	sql := "SELECT id FROM denglu_shuju WHERE aid=$1 AND jslx=$2 AND dlyxq>$3 AND fwpz=$4"
	// log.Println(sql)
	return DBConn.db.QueryRow(sql, w.aid, w.jslx, time.Now().Unix(), w.fwpz).Scan(&w.id)
}

func (w *denglu_shuju) SaveToDB(id int32) error {
	sql := ""
	var err error
	if id > 0 { //update
		sql = "UPDATE denglu_shuju set dlzt=$1,dlsj=$2,dlyxq=$3,fwpz=$4,jslx=$5,dlmc=$6,aid=$7 where id=$8 RETURNING id"
		err = DBConn.db.QueryRow(sql, w.dlzt, w.dlsj, w.dlyxq, w.fwpz, w.jslx, w.dlmc, w.aid, w.id).Scan(&w.id)
	} else { //insert
		sql = "INSERT INTO denglu_shuju(dlzt,dlsj,dlyxq,fwpz,jslx,dlmc,aid) VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id"
		err = DBConn.db.QueryRow(sql, w.dlzt, w.dlsj, w.dlyxq, w.fwpz, w.jslx, w.dlmc, w.aid).Scan(&w.id)
	}
	return err
}

func (w *denglu_shuju) SaveToDBByDlmc(dlmc string) error {
	sql := ""
	var err error
	sql = "UPDATE denglu_shuju set dlzt=$1,dlsj=$2,dlyxq=$3,fwpz=$4,jslx=$5,dlmc=$6,aid=$7 where dlmc='" + dlmc + "' RETURNING id"
	// log.Println(sql)
	// log.Println(w)
	err = DBConn.db.QueryRow(sql, w.dlzt, w.dlsj, w.dlyxq, w.fwpz, w.jslx, w.dlmc, w.aid).Scan(&w.id)
	if err != nil {
		//udpate failed, try to insert
		// log.Println(err, "try insert")
		sql = "INSERT INTO denglu_shuju(dlzt,dlsj,dlyxq,fwpz,jslx,dlmc,aid) VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id"
		err = DBConn.db.QueryRow(sql, w.dlzt, w.dlsj, w.dlyxq, w.fwpz, w.jslx, w.dlmc, w.aid).Scan(&w.id)
	}
	return err
}

func (w *denglu_shuju) SaveToDBByAccount(aid, lx int32) error {
	sql := ""
	var err error
	sql = "UPDATE denglu_shuju set dlzt=$1,dlsj=$2,dlyxq=$3,fwpz=$4,jslx=$5,dlmc=$6,aid=$7 where aid=" + fmt.Sprint(aid) + " AND jslx=" + fmt.Sprint(lx) + " RETURNING id"
	// log.Println(sql)
	// log.Println(w)
	err = DBConn.db.QueryRow(sql, w.dlzt, w.dlsj, w.dlyxq, w.fwpz, w.jslx, w.dlmc, w.aid).Scan(&w.id)
	if err != nil {
		//udpate failed, try to insert
		// log.Println(err, "try insert")
		sql = "INSERT INTO denglu_shuju(dlzt,dlsj,dlyxq,fwpz,jslx,dlmc,aid) VALUES($1,$2,$3,$4,$5,$6,$7) RETURNING id"
		err = DBConn.db.QueryRow(sql, w.dlzt, w.dlsj, w.dlyxq, w.fwpz, w.jslx, w.dlmc, w.aid).Scan(&w.id)
	}
	return err
}

func (w *denglu_shuju) fromPB(p *pb.DengLuShuJuL1) *denglu_shuju {
	w.id = p.GetID()
	w.dlzt = int32(p.GetDLZT())
	w.dlsj = p.GetDLSJ().GetSeconds()
	w.dlyxq = p.GetDLYXQ().GetSeconds()
	w.fwpz = p.GetFWPZ()
	w.jslx = int32(p.GetJSLX())
	w.dlmc = p.GetDLMC()
	w.aid = p.GetAID()
	return w
}

func (w *denglu_shuju) toPB() *pb.DengLuShuJuL1 {
	p := &pb.DengLuShuJuL1{
		ID:    w.id,
		DLZT:  pb.DengLuShuJuL1_DENGLUZHUANGTAI(w.dlzt),
		DLSJ:  &tm.Timestamp{Seconds: w.dlsj},
		DLYXQ: &tm.Timestamp{Seconds: w.dlsj},
		FWPZ:  w.fwpz,
		JSLX:  pb.YHLX(w.jslx),
		DLMC:  w.dlmc,
		AID:   w.aid,
	}
	return p
}
